// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async event => {
  const { _id } = event
  if (!_id) {
    throw new Error('no id')
  }

  const { data } = await db.collection('screen').doc(_id).get()
  ;({ data: data.casts } = await db
    .collection('screen-cast')
    .field({ _id: false, screen: false })
    .where({ screen: { _id } })
    .orderBy('week', 'asc')
    .get())

  return data
}
